layui.use(['form','layer', 'laypage'], function () {

    var $ = layui.$, form = layui.form, layer=layui.layer, laypage=layui.laypage;

    var page = 1;
    var limit = 40;
    var userInfo;
    $(function () {
        LoginStatus();

        userInfo = JSON.parse($.cookie('userInfo'));

        $('#userAvatar').attr('src', companyServices + userInfo.img);
        $('#userName').text((userInfo.name == '' || userInfo.name == null) ? "":userInfo.name);
        $('#userNameOther').text((userInfo.name == '' || userInfo.name == null ) ? "":userInfo.name);
        $('#userPhone').text((userInfo.tel == '' || userInfo.tel == null ) ? "":userInfo.tel);
        $('#userPosition').text(userInfo.position == '' ? '人事': userInfo.position);

        //加载公司信息
        loadCompany();
        //加载职位信息
        loadResumeList(0);
    })

    $('.right-part ul').on('click', 'li', function(e){
        var index = $(this).attr('data-index');
        $('.right-part ul li').removeClass('layui-this');
        $(this).addClass('layui-this');
        loadResumeList(index);
    });

    $('#logout').click(function () {
        layer.confirm('您确认退出登录吗？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            logoutCompany();
        }, function(){

        });
    })
    $('#addPosition').click(function () {
        window.location.href="job-manage.html";
    })
    //加载职位列表
    function loadResumeList(status) {
        layer.load(2);
        $.ajax({
            type: 'get', // 提交方式 get/post
            url: companyServices+"/resume/delivery/list?page="+page+"&limit=" + limit + "&status=" + status, // 需要提交的 url
            contentType:"application/json;charset=utf-8",
            xhrFields: {
                withCredentials: true
            }
            ,crossDomain: true
            ,data:{},
            success: function(res) {
                layer.closeAll('loading');
                checkLoginStatus(res);

                if(res.status == '00200'){
                    var resumeList = res.data;

                    var html = '';
                    if (resumeList != null && resumeList.length > 0) {
                        $.each(resumeList, function (index, item) {
                        html += '<li>';
                            html += '<a href="resume-detail.html?id='+item.id+'">';
                                html += '<div class="card-con1">';
                                html += '<div class="pic">';
                                html += '<img src="'+companyServices + item.userImg +'">';
                                html += '<i class="sex-icon">';
                                if (item.userGender == 2) {
                                    html += '<img src="../images/female1.png">';
                                } else {
                                    html += '<img src="../images/male1.png">';
                                }

                                html += '</i>';
                                html += '</div>';
                                html += '<div class="word-detail">';
                                html += '<h2>';
                                html += ((item.userName == "" || item.userName == null) ? "":item.userName) + '<span class="date">'+((item.userBirthday == "" || item.userBirthday == null ) ? "":item.userBirthday)+'</span>';
                                html += '</h2>';
                                html += '<p>'+((item.positionName == "" || item.positionName == null ) ? "":item.positionName)+'</p>';
                                if (item.status != 0 && item.status != 1) {
                                    html += '<div>';
                                    if (item.status == 3) {
                                        html += '<span class="accepted">已录取</span>';
                                    } else if (item.status == 2) {
                                        html += '<span class="accepted">待面试</span>';
                                    } else if (item.status == 4) {
                                        html += '<span class="accepted">不合适</span>';
                                    }
                                    html += '<div class="operator">';
                                    html += '<span>'+((item.operatorName == "" || item.operatorName == null) ? "":item.operatorName)+'</span>';
                                    html += '<i><img src="'+companyServices + item.operatorImg +'"></i>';
                                    html += '</div>';
                                }else if(item.status != 0 && item.status == 1) {
                                    html += '<div>';
                                    html += '<span class="not-view">待处理</span>';
                                    html += '</div>';
                                }else{
                                    html += '<div>';
                                    html += '<span class="not-view">未查看</span>';
                                    html += '</div>';
                                }

                                html += '</div>';
                                html += '</div>';
                                html += '</div>';
                                html += '<p class="card-con2">';
                                html += '<span>'+((item.workCity == "" || item.workCity == null ) ? "":item.workCity)+'</span>';
                                html += '<span>'+((item.userAge == "" || item.userAge == null) ? "":item.userAge)+'</span>';
                                html += '<span>工作'+((item.workYear == "" || item.workYear == null) ? 0:item.workYear)+'年</span>';
                                html += '<span></span>';
                                html += '</p>';
                                html += '</a>';
                                html += '</li>';
                        })



                    }

                    if (html.length == 0) {
                        $('.card-list').html("暂无数据");
                    } else {
                        $('.card-list').html(html);
                    }

                }
            }, fail:function () {
                layer.closeAll('loading');
            }
        })
    }

    //加载公司信息
    function loadCompany() {
        $.ajax({
            type: 'get', // 提交方式 get/post
            url: companyServices+"/company", // 需要提交的 url
            contentType:"application/json;charset=utf-8",
            xhrFields: {
                withCredentials: true
            }
            ,crossDomain: true
            ,data:{},
            success: function(res) {
                checkLoginStatus(res);

                if(res.status == '00200'){
                    var companyInfo = res.data;
                    //$.cookie("companyInfo", JSON.stringify(companyInfo));
                    $('#companyLogo').attr('src', companyServices + companyInfo.logo);
                    $('#companyFullName').text((companyInfo.fullName == '' || companyInfo.fullName == null) ? "":companyInfo.fullName);
                    $('#companySimpleName').text((companyInfo.simpleName == '' || companyInfo.simpleName == null) ? "":companyInfo.simpleName);
                    $('#industry').text((companyInfo.industryName == '' || companyInfo.industryName == null ) ? "":companyInfo.industryName);
                    $('#companyLevel').text((companyInfo.companySizeLevelName == '' || companyInfo.companySizeLevelName == null) ? "":companyInfo.companySizeLevelName);


                    if (companyInfo.rootId === userInfo.id) {
                        $('#hrManager').show();
                    }
                }
            }
        })
    }


    /**************************************时间格式化处理************************************/
    function dateFormat(fmt,date) {
        var o = {
            "M+" : date.getMonth()+1,                 //月份
            "d+" : date.getDate(),                    //日
            "h+" : date.getHours(),                   //小时
            "m+" : date.getMinutes(),                 //分
            "s+" : date.getSeconds(),                 //秒
            "q+" : Math.floor((date.getMonth()+3)/3), //季度
            "S"  : date.getMilliseconds()             //毫秒
        };
        if(/(y+)/.test(fmt))
            fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
        for(var k in o)
            if(new RegExp("("+ k +")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        return fmt;
    }
});




